package com.wkk.yuli.aop;

import com.wkk.yuli.util.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;

import java.util.regex.Pattern;

/**
 * @author : WangKeKe
 * @date : 2020/9/25 15:04
 * Description : 实现AOP持久化
 */


@Aspect
@Slf4j
@Configuration
public class PersistenceAspect {


    private static final Pattern METHOD_TITLE = Pattern.compile("^((set)|(remove)).*$");

    @Pointcut("execution(* com.wkk.yuli.api.YuTemplate.*(..))")
    private void pushKey() {
    }

    @Around("pushKey()")
    private Object pushToText(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();
        // 执行方法体
        Object result = joinPoint.proceed();
        // 方法名携带要求字符，且开启持久化以及策略为每次刷新
        if (METHOD_TITLE.matcher(methodName).matches() && ToolUtils.push()) {
            log.info("执行方法 : [ " + methodName + " ] :  刷新持久化文件yl.txt 的 数据");
            log.info("刷新持久化文件yl.txt >>>> end 耗时：" + (System.currentTimeMillis() - beginTime) + "ms.");
        }
        return result;
    }
}
